﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Saturn.App.Common;
using Saturn.App.FormOper;
using System.Data;
using System.Collections;
using System.Reflection;

namespace Saturn.App
{
    public partial class FormMain : Form
    {

        public FormMain()
        {

            InitializeComponent();
            int j = LiView.Items.Count;

            if (!CommonCfg.GetSystemConfig())
            {

            }
            //InitListView();

            #region 根据权限控制桌面按钮的显示
            try
            {
                var li = new List<ListViewItem>();

                for (int i = 0; i < j; i++)
                {
                    ListViewItem item = LiView.Items[i];

                    if (!UserLogin.roleName.Contains(item.Text.ToString()))
                    {
                        //每个用户都有注销功能
                        if (Convert.ToInt32(item.Tag) != (int)EnumClass.Features.Logon &&
                            Convert.ToInt32(item.Tag) != (int)EnumClass.Features.Logon)
                        {
                            li.Add(item);
                        }
                    }
                }

                if (li.Any())
                {
                    foreach (var item in li)
                    {
                        LiView.Items.Remove(item);
                    }
                }


            }
            catch (Exception ex)
            {
                //MessageBox.Show("权限错误！");
            }
            #endregion

            //statusBar.Text = "保管员：" + UserLogin.userRealName + "(" + UserLogin.loginName + ") " + DateTime.Now.ToString("yyyy年MM月dd日");
            statusBar.Text ="操作员："+ CommonCfg.Userrealname + DateTime.Now.ToString("yyyy年MM月dd日");

        }


        #region 美化listview

        const int GWL_STYLE = -16;
        const int WS_BORDER = 0x00800000;
        const int SWP_NOSIZE = 0x1;
        const int SWP_NOMOVE = 0x2;
        const int SWP_FRAMECHANGED = 0x20;

        [DllImport("coredll.dll")]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("coredll.dll")]
        private extern static void SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);

        [DllImport("coredll.dll")]
        private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int uflags);

        private void ShowBorder(IntPtr handle, bool bShow)
        {
            int style = GetWindowLong(handle, GWL_STYLE);
            if (bShow)
            {
                style |= WS_BORDER;
            }
            else
            {
                style &= ~WS_BORDER;
            }
            SetWindowLong(handle, GWL_STYLE, style);
            SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
        }

        private void InitListView()
        {
            ShowBorder(LiView.Handle, false);
        }
        #endregion


        #region 功能选择事件
        /// <summary>
        /// 功能选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvOut_ItemActivate(object sender, EventArgs e)
        {
            if (this.LiView.FocusedItem != null)
            {
                //int OutOrderType = Convert.ToInt32(this.LiView.FocusedItem.Tag);
                EnumClass.Features fea = (EnumClass.Features)Enum.ToObject(typeof(EnumClass.Features), Convert.ToInt32(this.LiView.FocusedItem.Tag));
                switch (fea)
                {
                    case EnumClass.Features.weixiu:
                        new FormWx().ShowDialog(); // 维修
                        break;
                    case EnumClass.Features.baoyang:
                        new FormBy().ShowDialog(); // 保养
                        break;
                    case EnumClass.Features.dianjian:
                        //new FormDj().ShowDialog(); //点检
                        break;
                    case EnumClass.Features.runhua:
                        //new FormRh().ShowDialog(); // 润滑
                        break;
                    case EnumClass.Features.touliao:
                        new FormCheck().ShowDialog(); // 辅料确认
                        break;
                    case EnumClass.Features.peizhi:
                        new FormLineConfig().ShowDialog(); // 系统配置
                        break;
                    case EnumClass.Features.billadd:
                        new FormBillAdd().ShowDialog(); // 添加原辅料订单
                        break;  
                    case EnumClass.Features.Logon://注销
                        #region 注销
                        DialogResult dr = MessageBox.Show("确认退出注销系统吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                        if (dr == DialogResult.Yes)
                        {
                            this.Close();

                            //GetScanData.RemoveScan();

                            System.Diagnostics.Process.GetCurrentProcess().Kill();
                        }
                        #endregion
                        break;
                }
            }

        }
        #endregion

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            #region 注销
            DialogResult dr = MessageBox.Show("确认退出注销系统吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            if (dr == DialogResult.Yes)
            {
                this.Close();

                //GetScanData.RemoveScan();

                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            #endregion
        }

        private void btn_touliao_Click(object sender, EventArgs e)
        {
            new FormCheck().ShowDialog(); // 辅料确认
        }

        private void btn_baoyang_Click(object sender, EventArgs e)
        {
            new FormBy().ShowDialog(); // 保养
        }

        private void btn_runhua_Click(object sender, EventArgs e)
        {
            //new FormRh().ShowDialog(); // 润滑
        }

        private void btn_dianjian_Click(object sender, EventArgs e)
        {
            //new FormDj().ShowDialog(); //点检
        }

        private void btn_weixiu_Click(object sender, EventArgs e)
        {
            new FormWx().ShowDialog(); // 维修
        }

        private void btn_peizhi_Click(object sender, EventArgs e)
        {
            new FormLineConfig().ShowDialog(); // 系统配置
        }

        private void btn_dingdan_Click(object sender, EventArgs e)
        {
            new FormBillAdd().ShowDialog(); // 添加原辅料订单
        }

        private void btn_guanyu_Click(object sender, EventArgs e)
        {

        }

        

      



      



    }
}